package com.lgl.scsheader.system.controller;

import com.alibaba.fastjson.JSON;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.system.model.Role;
import com.lgl.scsapi.util.utils.Constants;
import com.lgl.scsapi.util.utils.Tools;
import com.lgl.scsheader.base.controller.BaseController;
import com.lgl.scsheader.system.model.Page;
import com.lgl.scsheader.system.model.SysMenu;
import com.lgl.scsheader.system.service.MenuService;
import com.lgl.scsheader.system.service.RoleService;
import com.lgl.scsheader.util.utils.RightsHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
 * 类名称：角色Controller
 */
@Controller
@RequestMapping(value="/role")
public class RoleController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(RoleController.class);

	@Resource(name="menuService")
	private MenuService menuService;
	@Resource(name="roleService")
	private RoleService roleService;
	
	/**
	 * 权限(增删改查)
	 */
	@RequestMapping(value="/qx")
	public ModelAndView qx(){
		try{
			PageData pd = this.getPageData();
			String msg = pd.getString("msg");
			roleService.updateQx(msg,pd);
			
			//mv.setViewName(PAGE_PATH_PREFIX + "save_result.jsp");
			//mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return null;
	}

	/**
	 * c权限
	 */
	@RequestMapping(value="/gysqxc")
	public ModelAndView gysqxc(){
		//ModelAndView mv = this.getModelAndView();
		try{
			PageData pd = this.getPageData();
			String msg = pd.getString("msg");
			roleService.gysqxc(msg,pd);
			
			//mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return null;
	}
	
	/**
	 * 列表
	 */
	@RequestMapping
	public ModelAndView list(Page page){
		//ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();

		String roleId = pd.getString("ROLE_ID");
		if(roleId == null || "".equals(roleId)){
			pd.put("ROLE_ID", "1");
		}
		List<Role> roleList = roleService.listAllRoles();				//列出所有部门
		List<Role> roleList_z = roleService.listAllRolesByPId(pd);		//列出此部门的所有下级

		List<PageData> kefuqxlist = roleService.listAllkefu(pd);		//管理权限列表
		List<PageData> gysqxlist = roleService.listAllGysQX(pd);		//用户权限列表
		pd = roleService.findObjectById(pd);							//取得点击部门
		/*mv.addObject("pd", pd);
		mv.addObject("kefuqxlist", kefuqxlist);
		mv.addObject("gysqxlist", gysqxlist);
		mv.addObject("roleList", roleList);
		mv.addObject("roleList_z", roleList_z);
		//mv.setViewName(PAGE_PATH_PREFIX + "system/role/role_list.jsp");
		mv.addObject(Constants.SESSION_QX,this.getHC());*/	//按钮权限
		return null;
	}

	/**
	 * 保存新增信息
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(){
		//ModelAndView mv = this.getModelAndView();
		try{
			PageData pd = this.getPageData();
			
			String parent_id = pd.getString("PARENT_ID");		//父类角色id
			pd.put("ROLE_ID", parent_id);			
			if("0".equals(parent_id)){
				pd.put("RIGHTS", "");
			}else{
				String rights = roleService.findObjectById(pd).getString("RIGHTS");
				pd.put("RIGHTS", (null == rights)?"":rights);
			}
			pd.put("QX_ID", "");
			String UUID = Tools.getUUID();
			pd.put("GL_ID", UUID);
			pd.put("FX_QX", 0);				//发信权限
			pd.put("FW_QX", 0);				//服务权限
			pd.put("QX1", 0);				//操作权限
			pd.put("QX2", 0);				//产品权限
			pd.put("QX3", 0);				//预留权限
			pd.put("QX4", 0);				//预留权限
			roleService.saveKeFu(pd);		//保存到K权限表

			pd.put("U_ID", UUID);
			pd.put("C1", 0);				//每日发信数量
			pd.put("C2", 0);
			pd.put("C3", 0);
			pd.put("C4", 0);
			pd.put("Q1", 0);				//权限1
			pd.put("Q2", 0);				//权限2
			pd.put("Q3", 0);
			pd.put("Q4", 0);
			roleService.saveGYSQX(pd);		//保存到G权限表
			pd.put("QX_ID", UUID);
			
			pd.put("ROLE_ID", UUID);
			pd.put("ADD_QX", "0");
			pd.put("DEL_QX", "0");
			pd.put("EDIT_QX", "0");
			pd.put("CHA_QX", "0");
			roleService.add(pd);
			//mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
			//mv.addObject("msg","failed");
		}
		return null;
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit(){
		//ModelAndView mv = this.getModelAndView();
		try{
			PageData pd = this.getPageData();
			roleService.edit(pd);
			//mv.addObject("msg","success");
		} catch(Exception e){
			logger.error(e.toString(), e);
			//mv.addObject("msg","failed");
		}
		return null;
	}
	
	/**
	 * 请求角色菜单授权页面
	 */
	@RequestMapping(value="/auth")
	public String auth(@RequestParam String ROLE_ID,Model model){
		
		try{
			List<SysMenu> menuList = menuService.listAllMenu();
			Role role = roleService.getRoleById(ROLE_ID);
			String roleRights = role.getRights();
			/*if(!StringUtils.isEmpty(roleRights)){
				for(SysMenu menu : menuList){
					menu.setHasMenu(RightsHelper.testRights(roleRights, menu.getMENU_ID()));
					if(menu.isHasMenu()){
						List<SysMenu> subMenuList = menu.getSubMenu();
						for(SysMenu sub : subMenuList){
							sub.setHasMenu(RightsHelper.testRights(roleRights, sub.getMENU_ID()));
						}
					}
				}
			}*/
			/*JSONArray arr = JSONArray.fromObject(menuList);
			String json = arr.toString();*/
			String json = JSON.toJSONString(menuList);
			json = json.replaceAll("MENU_ID", "id").replaceAll("MENU_NAME", "name").replaceAll("subMenu", "nodes").replaceAll("hasMenu", "checked");
			model.addAttribute("zTreeNodes", json);
			model.addAttribute("roleId", ROLE_ID);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return "authorization.jsp";
	}
	
	/**
	 * 请求角色按钮授权页面
	 */
	@RequestMapping(value="/button")
	public ModelAndView button(@RequestParam String ROLE_ID,@RequestParam String msg,Model model){
		//ModelAndView mv = this.getModelAndView();
		try{
			List<SysMenu> menuList = menuService.listAllMenu();
			Role role = roleService.getRoleById(ROLE_ID);
			
			String roleRights = "";
			if("add_qx".equals(msg)){
				roleRights = role.getAddQx();
			}else if("del_qx".equals(msg)){
				roleRights = role.getDelQx();
			}else if("edit_qx".equals(msg)){
				roleRights = role.getEditQx();
			}else if("cha_qx".equals(msg)){
				roleRights = role.getChaQx();
			}
			
			/*if(!StringUtils.isEmpty(roleRights)){
				for(SysMenu menu : menuList){
					menu.setHasMenu(RightsHelper.testRights(roleRights, menu.getMENU_ID()));
					if(menu.isHasMenu()){
						List<SysMenu> subMenuList = menu.getSubMenu();
						for(SysMenu sub : subMenuList){
							sub.setHasMenu(RightsHelper.testRights(roleRights, sub.getMENU_ID()));
						}
					}
				}
			}*/
			/*JSONArray arr = JSONArray.fromObject(menuList);
			String json = arr.toString();*/
			String json = JSON.toJSONString(menuList);
			json = json.replaceAll("MENU_ID", "id").replaceAll("MENU_NAME", "name").replaceAll("subMenu", "nodes").replaceAll("hasMenu", "checked");
			/*mv.addObject("zTreeNodes", json);
			mv.addObject("roleId", ROLE_ID);
			mv.addObject("msg", msg);*/
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		//mv.setViewName(PAGE_PATH_PREFIX + "system/role/role_button.jsp");
		return null;
	}
	
	/**
	 * 保存角色菜单权限
	 */
	@RequestMapping(value="/auth/save")
	public void saveAuth(@RequestParam String ROLE_ID,@RequestParam String menuIds,PrintWriter out){
		PageData pd = new PageData();
		try{
			if(null != menuIds && !"".equals(menuIds.trim())){
				BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
				Role role = roleService.getRoleById(ROLE_ID);
				role.setRights(rights.toString());
				roleService.updateRoleRights(role);
				pd.put("rights",rights.toString());
			}else{
				Role role = new Role();
				role.setRights("");
				role.setRoleId(ROLE_ID);
				roleService.updateRoleRights(role);
				pd.put("rights","");
			}
			pd.put("roleId", ROLE_ID);
			roleService.setAllRights(pd);
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
	}
	
	/**
	 * 保存角色按钮权限
	 */
	@RequestMapping(value="/roleButton/save")
	public void orleButton(@RequestParam String ROLE_ID,@RequestParam String menuIds,@RequestParam String msg,PrintWriter out){
		PageData pd = this.getPageData();
		try{
			if(null != menuIds && !"".equals(menuIds.trim())){
				BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
				pd.put("value",rights.toString());
			}else{
				pd.put("value","");
			}
			pd.put("ROLE_ID", ROLE_ID);
			roleService.updateQx(msg,pd);
			
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object deleteRole(@RequestParam String ROLE_ID){
		Map<String,String> map = new HashMap<String,String>();
		PageData pd = new PageData();
		String errInfo = "";
		try{
			pd.put("ROLE_ID", ROLE_ID);
			List<Role> roleList_z = roleService.listAllRolesByPId(pd);		//列出此部门的所有下级
			if(roleList_z.size() > 0){
				errInfo = "false";
			}else{
				
				List<PageData> userlist = roleService.listAllUByRid(pd);
				List<PageData> appuserlist = roleService.listAllAppUByRid(pd);
				if(userlist.size() > 0 || appuserlist.size() > 0){
					errInfo = "false2";
				}else{
					roleService.deleteRoleById(ROLE_ID);
					roleService.deleteKeFuById(ROLE_ID);
					roleService.deleteGById(ROLE_ID);
					errInfo = "success";
				}
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		map.put("result", errInfo);
		return map;
	}

	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Constants.SESSION_QX);
	}
	/* ===============================权限================================== */

}
